<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\middleware\Auth;
use app\model\RedisModel;
use app\model\ShopReturnAddressesModel;
use app\model\ShopsModel;
use think\App;

class ShopReturnAddresses extends Base {

    protected $middleware = [Auth::class];

    public $cacheKey="shop:return:address:id:";

    private $aid;

    private $shopsModel;

    private $shopReturnAddressesModel;

    private $req;

    private $page;

    private $limit;

    private $shop_id;

    private $shopIds;


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->shop_id = isset($this->req['shop_id']) ? $this->req['shop_id'] : "";

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit'] : 20;

        $this->shopsModel = new ShopsModel();

        $this->aid = $this->isLogin();

        $this->shopIds = $this->shopsModel->getShopIds($this->aid);

        $this->shopReturnAddressesModel = new ShopReturnAddressesModel();

    }


    /*
     * 获取店铺列表
     * */
    public function getList(){
        $category = !isset($this->req["category"]) ? "" : $this->req["category"];
        $where=[];
        $where['shop_id'] = $this->shop_id;
        if(!empty($category)){
            $where['category'] = $category;
        }

        $list = $this->shopReturnAddressesModel->getList($where,$this->page,$this->limit);

        $total =  $this->shopReturnAddressesModel->getTotal($where);

        return $this->succData($total,$list);
    }



    /*
     * 添加
     * */
    public function add()
    {

        if(empty($this->req['province'])){
            return $this->fail("省份不能为空");
        }

        if(empty($this->req['city'])){
            return $this->fail("市不能为空");
        }

        if(empty($this->req['county'])){
            return $this->fail("区不能为空");
        }

        if(empty($this->req['address'])){
            return $this->fail("详细地址不能为空");
        }

        if(empty($this->req['name'])){
            return $this->fail("收件人不能为空");
        }

        if(!verification_mobile($this->req['mobile'])){
            return $this->fail("手机号不合法");
        }

        if(!in_array($this->req['category'],[1,2])){
            return $this->fail("请选择发货地址还是退货地址");
        }

        //删除用户默认地址
        if($this->req['is_default']){
            $this->shopReturnAddressesModel->cancelShopDefaultAddress($this->req["shop_id"],$this->req["category"]);
            $this->delAllCache();
        }

        $where = [];
        $where[] = ["category","=",$this->req['category']];
        $where[] = ["shop_id","=",$this->req["shop_id"]];
        $where[] = ["province","=",$this->req["province"]];
        $where[] = ["city","=",$this->req["city"]];
        $where[] = ["county","=",$this->req["county"]];
        $where[] = ["name","=",$this->req["name"]];
        $where[] = ["address","=",$this->req["address"]];
        $where[] = ["mobile","=",$this->req["mobile"]];
        if(isset( $this->req['shop_return_addresses_id'])){
            $where[] = ["shop_return_addresses_id","<>",$this->req["shop_return_addresses_id"]];
            if( $this->shopReturnAddressesModel->getTotal($where) > 0){
                return $this->fail("信息相同地址已存在，不可修改为此地址");
            }
        }else{
            if( $this->shopReturnAddressesModel->getTotal($where) > 0){
                return $this->fail("信息相同地址已存在，不可修改为此地址");
            }
        }

        $shop_return_addresses_id = $this->shopReturnAddressesModel->addOne($this->req);

        if(isset($this->req["shop_return_addresses_id"])){

            $this->delCache($this->req["shop_return_addresses_id"]);

            return $this->succ($this->shopReturnAddressesModel->getInfo($this->req["shop_return_addresses_id"]));

        }else{

            return $this->succ($this->shopReturnAddressesModel->getInfo($shop_return_addresses_id));

        }
    }


    /*
     * 店铺审核
     * */
    public function del(){

        if(!in_array($this->shop_id,$this->shopIds)){
            return $this->fail("店铺不存在");
        }

        $where=[];
        $where['shop_return_addresses_id'] = $this->req["shop_return_addresses_id"];
        $res = $this->shopReturnAddressesModel->delAll($where);

        if($res){

            $this->delCache($this->req["shop_return_addresses_id"]);

            return $this->succ();

        }else{

            return $this->fail();

        }
    }


    /*
  * 设置/取消默认地址
  * */
    public function setShopDefaultAddress()
    {

        if(!in_array($this->shop_id,$this->shopIds)){
            return $this->fail("店铺不存在");
        }

        $info = $this->shopReturnAddressesModel->getInfo($this->req["shop_return_addresses_id"]);

        if(empty($info)){
            return $this->fail("地址不存在");
        }

        //删除用户默认地址
        if($this->req['is_default']){
            $this->shopReturnAddressesModel->cancelShopDefaultAddress($this->req["shop_id"],$info["category"]);
            $this->delAllCache();
        }

        $where=[];
        $where['shop_return_addresses_id'] = $this->req["shop_return_addresses_id"];
        $res = $this->shopReturnAddressesModel->updateAll($where,[
            "is_default"=>$this->req['is_default']
        ]);

        if($res){

            $this->delCache($this->req["shop_return_addresses_id"]);

            return $this->succ();

        }else{

            return $this->fail();

        }
    }


    public function delCache($id)
    {
        $redisModel = new RedisModel();

        return $redisModel->delete($this->cacheKey.$id);
    }


    public function delAllCache()
    {
        $redisModel = new RedisModel();

        return $redisModel->deleteAll($this->cacheKey);
    }
}
